//
//  O2MainController.swift
//  O2Platform
//
//  Created by FancyLou on 2019/1/25.
//  Copyright © 2019 zoneland. All rights reserved.
//

import UIKit
import CocoaLumberjack
import Starscream
import AudioToolbox

class O2MainController: O2BaseForRotateUITabBarController, UITabBarControllerDelegate {

    static var tabBarVC: O2MainController!

    static func genernateVC() -> O2MainController {
        return O2MainController()
    }

    private var isSimple = false
    private var currentIndex: Int = 0
    // demo服务器弹出公告
    private var demoAlertView = O2DemoAlertView()
    private let viewModel: OOLoginViewModel = {
        return OOLoginViewModel()
    }()
    private lazy var mainViewModel: O2MainViewModel = {
        return O2MainViewModel()
    }()
    //获取消息数量
    private lazy var imViewModel: IMViewModel = {
        return IMViewModel()
    }()
    //考勤判断版本
    private lazy var attendanceViewModel: OOAttandanceViewModel = {
        return OOAttandanceViewModel()
    }()
    // 云盘
    private lazy var cFileVM: CloudFileViewModel = {
        return CloudFileViewModel()
    }()
    // 论坛
    private lazy var bbsVm: BBSViewModel = {
        return BBSViewModel()
    }()
    private var pages: [MainPageType] = []
    private let barIm = L10n.mainBarIm
    private let barContact = L10n.mainBarContacts
    private let barApps = L10n.mainBarApps
    private let barSettings = L10n.mainBarSettings


    override func viewDidLoad() {
        super.viewDidLoad()
        // 回到前端 执行下极速打卡
        NotificationCenter.default.addObserver(self, selector: #selector(doFastCheckIn), name: UIApplication.willEnterForegroundNotification, object: nil)
        if UIDevice.deviceModelReadable() != "Simulator" {
            self.checkAppVersion()
        }
        //查询通讯录权限
        self.mainViewModel.loadOrgContactPermission()
        
        self.tabBar.tintColor = O2ThemeManager.color(for: "Base.base_color")!
        self.delegate = self
        // 配置是否简易模式
        if let mode = O2AuthSDK.shared.customStyle()?.simpleMode, mode == true {
            DDLogDebug("进入简易模式！！！！！！！")
            isSimple = true
        }
        self.pages.removeAll()
        if let pageSet = O2AuthSDK.shared.customStyle()?.appIndexPages, !pageSet.isEmpty {
            pageSet.forEach { p in
                if (p == MainPageType.home.getKey()) {
                    self.pages.append(MainPageType.home)
                }
                if (p == MainPageType.im.getKey()) {
                    self.pages.append(MainPageType.im)
                }
                if (p == MainPageType.contact.getKey()) {
                    self.pages.append(MainPageType.contact)
                }
                if (p == MainPageType.app.getKey()) {
                    self.pages.append(MainPageType.app)
                }
                if (p == MainPageType.settings.getKey()) {
                    self.pages.append(MainPageType.settings)
                }
            }
            if !self.pages.contains(MainPageType.home) {
                self.pages.append(MainPageType.home)
            }
            if !self.pages.contains(MainPageType.settings) {
                self.pages.append(MainPageType.settings)
            }
        } else {
            if isSimple {
                self.pages.append(MainPageType.home)
                self.pages.append(MainPageType.settings)
            } else {
                self.pages.append(MainPageType.home)
                self.pages.append(MainPageType.im)
                self.pages.append(MainPageType.contact)
                self.pages.append(MainPageType.app)
                self.pages.append(MainPageType.settings)
            }
        }
        
        self.pages.sort { a, b in
            return a.getOrder() < b.getOrder()
        }
        var controllers : [UIViewController] = []
        for p in self.pages {
            if p == MainPageType.home {
                // main
                let mainVC = mainController()
                mainVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "icon_zhuye_nor"), selectedImage: O2ThemeManager.image(for: "Icon.icon_zhuye_pre"))
                mainVC.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
                let blurImage = OOCustomImageManager.default.loadImage(.index_bottom_menu_logo_blur)
                let newBlurImage = blurImage?.withRenderingMode(.alwaysOriginal)
                mainVC.tabBarItem.image = newBlurImage
                let focusImage = OOCustomImageManager.default.loadImage(.index_bottom_menu_logo_focus)
                let newFocusImage = focusImage?.withRenderingMode(.alwaysOriginal)
                mainVC.tabBarItem.selectedImage = newFocusImage
                controllers.append(mainVC)
            }
            if p == MainPageType.im {
                //消息
                let conversationVC = IMConversationListViewController()
                conversationVC.title = barIm
                let messages = ZLNavigationController(rootViewController: conversationVC)
                messages.tabBarItem = UITabBarItem(title: barIm, image: UIImage(named: "icon_news_nor"), selectedImage: O2ThemeManager.image(for: "Icon.icon_news_pre"))
                controllers.append(messages)
            }
            if p == MainPageType.contact {
                //通讯录
                let addressVC = OOTabBarHelper.getVC(storyboardName: "contacts", vcName: nil)
                let address = ZLNavigationController(rootViewController: addressVC)
                address.tabBarItem = UITabBarItem(title: barContact, image: UIImage(named: "icon_address_g"), selectedImage: O2ThemeManager.image(for: "Icon.icon_address_list_pro"))
                controllers.append(address)
            }
            if p == MainPageType.app {
                //应用
                let appsVC = OOTabBarHelper.getVC(storyboardName: "apps", vcName: nil)
                let apps = ZLNavigationController(rootViewController: appsVC)
                apps.tabBarItem = UITabBarItem(title: barApps, image: UIImage(named: "icon_yingyong"), selectedImage: O2ThemeManager.image(for: "Icon.icon_yingyong_pro"))
                controllers.append(apps)
            }
            if p == MainPageType.settings {
                //设置
                let settingsVC = OOTabBarHelper.getVC(storyboardName: "setting", vcName: nil)
                let settings = ZLNavigationController(rootViewController: settingsVC)
                settings.tabBarItem = UITabBarItem(title: barSettings, image: UIImage(named: "setting_normal"), selectedImage: O2ThemeManager.image(for: "Icon.setting_selected"))
                controllers.append(settings)
            }
        }
        self.viewControllers = controllers
        var cIndex = 0
        for p in self.pages {
            if p == MainPageType.home {
                break
            }
            cIndex += 1
        }
        selectedIndex = cIndex
        currentIndex = cIndex
        
        // 极光
        O2JPushManager.shared.o2JPushBind()
        //连接websocket
        self._startWebsocket()
        //读取消息
        self.getConversationList()
        //检查考勤版本
        self.checkAttendanceVersion()
        //检查云盘版本
        self.checkCloudFileVersion()
        // 论坛禁言问题
        self.checkBBSMuteInfo()
        // 极速打卡
        self.doFastCheckIn()
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
        //关闭websocket
        self._stopWebsocket()
    }

    override func viewDidAppear(_ animated: Bool) {
        // 判断是否 第一次安装 是否是连接的demo服务器
        if let unit = O2AuthSDK.shared.bindUnit() {
            if "demo.o2oa.net" == unit.centerHost || "demo.o2oa.io" == unit.centerHost || "demo.o2server.io" == unit.centerHost || "sample.o2oa.net" == unit.centerHost {
                let tag = AppConfigSettings.shared.demoAlertTag
                if !tag {
                    demoAlertView.showFallDown()
                    AppConfigSettings.shared.demoAlertTag = true
                }
            }
        }
    }

    //MARK: -- delegate
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        self.currentIndex = tabBarController.selectedIndex
    }
  

    private func mainController() -> UIViewController {
        let appid = O2AuthSDK.shared.customStyle()?.indexPortal
        let indexType = O2AuthSDK.shared.customStyle()?.indexType ?? "default"
        if indexType == "portal" {
            let app = DBManager.shared.queryData(appid!)
            let destVC = OOTabBarHelper.getVC(storyboardName: "apps", vcName: "OOMainWebVC")
            if let mail = destVC as? MailViewController {
                mail.app = app
                mail.isIndexShow = true
                let nav = ZLNavigationController(rootViewController: mail)
                return nav
            } else {
                let nav = ZLNavigationController(rootViewController: destVC)
                return nav
            }
        } else {
            let destVC = OOTabBarHelper.getVC(storyboardName: "task", vcName: nil)
            let nav = ZLNavigationController(rootViewController: destVC)
            return nav
        }
    }
    
    // MARK: - 论坛 禁言问题查询
    private func checkBBSMuteInfo() {
        self.bbsVm.getMuteInfo().then { info in
            O2AuthSDK.shared.setupMuteInfo(muteInfo: info)
        }.catch { err in
            O2AuthSDK.shared.setupMuteInfo(muteInfo: nil)
        }
    }
    
    // MARK: - 考勤判断版本
    private func checkAttendanceVersion() {
        self.attendanceViewModel.checkVersion { version in
            if let v = version?.version, v == "2" {
                StandDefaultUtil.share.userDefaultCache(value: "2" as AnyObject, key: O2.O2_Attendance_version_key)
                DDLogInfo("考勤v2")
            } else {
                DDLogInfo("老版考勤")
                StandDefaultUtil.share.userDefaultCache(value: "1" as AnyObject, key: O2.O2_Attendance_version_key)
            }
        }
    }
    /// 极速打卡
    @objc private func doFastCheckIn() {
        FastCheckInManager.shared.start()
    }
    
    // MARK: - 云盘判断是否v3版本
    private func checkCloudFileVersion() {
        self.cFileVM.v3Echo().then { result in
            StandDefaultUtil.share.userDefaultCache(value: result as AnyObject, key: O2.O2CloudFileVersionKey)
        }.catch { error in
            StandDefaultUtil.share.userDefaultCache(value: false as AnyObject, key: O2.O2CloudFileVersionKey)
            DDLogError(error.localizedDescription)
        }
    }

    
    // MARK: - IM message
    func getConversationList() {
        imViewModel.myConversationList().then { (list) in
            var n = 0
            if !list.isEmpty {
                for item in list {
                    if let number = item.unreadNumber {
                        n += number
                    }
                }
            }
            DispatchQueue.main.async {
                DDLogDebug("消息数量: \(n)")
                if n > 0 && n < 100 {
                    self.showRedPoint(number: "\(n)")
                } else if n >= 100 {
                    self.showRedPoint(number: "99..")
                }
            }
        }
    }

    private func showRedPoint(number: String) {
        self.viewControllers?.forEach({ (vc) in
            if let zl = vc as? ZLNavigationController, zl.tabBarItem?.title == barIm {
                zl.tabBarItem.badgeValue = number
            }
        })
    }
    
    //消息模块未读消息数量加1
    private func addUnreadNumber() {
        self.viewControllers?.forEach({ (vc) in
            if let zl = vc as? ZLNavigationController, zl.tabBarItem?.title == barIm {
                if let badge = zl.tabBarItem.badgeValue {
                    if badge != "99.." {
                        if let n = Int(string: badge) {
                            let number = n + 1
                            if number > 0 && number < 100 {
                                self.showRedPoint(number: "\(number)")
                            } else if number >= 100 {
                                self.showRedPoint(number: "99..")
                            }
                        }
                    }
                }else {
                    self.showRedPoint(number: "1")
                }
            }
        })
    }

    
    // MARK: - app update 
    private func checkAppVersion() {
        O2VersionManager.shared.checkAppUpdate { (info, error) in
            if let iosInfo = info {
                DDLogDebug(iosInfo.toJSONString() ?? "")
                let alertController = UIAlertController(title: "版本更新", message: "更新内容：\(iosInfo.content ?? "")", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "确定", style: .default, handler: { ok in
                    O2VersionManager.shared.updateAppVersion(info?.downloadUrl)
                })
                let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { c in
                    //
                })
                alertController.addAction(cancelAction)
                alertController.addAction(okAction)
                UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
            } else {
                DDLogInfo("没有版本更新：\(error ?? "")")
            }
        }
    }



    // MARK: - websocket
    private var timer: Timer?
    private var isWsOpen = false

    private func _startWebsocket() {
        DDLogDebug("启动websocket连接。。。。。。")
        let url = AppDelegate.o2Collect.generateWebsocketURL()
        DDLogDebug("这个是wsurl ：\(url)")
        O2WebsocketManager.instance.startConnect(wsUrl: url, delegate: self)
    }

    private func _stopWebsocket() {
        DDLogDebug("关闭websocket连接。。。。。。")
        self.stopTiming()
        O2WebsocketManager.instance.closeConnect()
    }

    private func startTiming() {
        DDLogDebug("开启定时器 。。。。。。")
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
        timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(sendHeartbeatMsg), userInfo: nil, repeats: true)
        timer?.fire()
    }
    private func stopTiming() {
        DDLogDebug("关闭定时器 。。。。。。")
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
    }
    //发送心跳
    @objc private func sendHeartbeatMsg() {
        if isWsOpen {
            O2WebsocketManager.instance.send(msg: o2_im_ws_heartbeat)
        } else { //重新启动
            _startWebsocket()
        }
    }

}

extension O2MainController: WebSocketDelegate {


    func didReceive(event: WebSocketEvent, client: WebSocket) {
        switch event {
        case .text(let text):
            if text != o2_im_ws_heartbeat { //忽略心跳消息
                DDLogDebug("接收的ws消息：\(text)")
                //判断type im消息就发送通知
                do {
                    if let dicArr = try JSONSerialization.jsonObject(with: String(text).data(using: .utf8)!, options: .allowFragments) as? [String: AnyObject] {
                        guard let type = dicArr["type"] as? String else {
                            return
                        }
                        if type == O2.O2_MESSAGE_TYPE_IM_CREATE {
                            if let messageInfo = WsImMessage.deserialize(from: text) {
                                DDLogDebug("接收到im消息 发送通知。。")
                                NotificationCenter.post(customeNotification: OONotification.imCreate, object: messageInfo.body)
                            }
                            self.addUnreadNumber()
                            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
                        } else if type == O2.O2_MESSAGE_TYPE_IM_REVOKE {
                            if let messageInfo = WsImMessage.deserialize(from: text) {
                                DDLogDebug("接收到im消息 撤回消息")
                                NotificationCenter.post(customeNotification: OONotification.imRevoke, object: messageInfo.body)
                            }
                        } else if type == O2.O2_MESSAGE_TYPE_IM_CONVERSATION_UPDATE {
                            if let messageInfo = WsImConvMessage.deserialize(from: text) {
                                DDLogDebug("接收到im消息 更新会话消息")
                                NotificationCenter.post(customeNotification: OONotification.imConvUpdate, object: messageInfo.body)
                            }
                        } else if type == O2.O2_MESSAGE_TYPE_IM_CONVERSATION_DELETE {
                            if let messageInfo = WsImConvMessage.deserialize(from: text) {
                                DDLogDebug("接收到im消息 删除消息")
                                NotificationCenter.post(customeNotification: OONotification.imConvDelete, object: messageInfo.body)
                            }
                        }
                    }
                } catch { }
            }
            break
        case .connected(let headers):
            DDLogDebug("websocket is connected: \(headers)")
            isWsOpen = true
            self.startTiming()
            break
        case .disconnected(let reason, let code):
            DDLogDebug("websocket is disconnected: \(reason) with code: \(code)")
            isWsOpen = false
            break
        case .binary(let data):
            DDLogDebug("websocket binary Received  二进制对象: \(data.count)")
            break
        case .ping(_):
            break
        case .pong(_):
            break
//        case .viablityChanged(_):
//            DDLogDebug("websocket viablityChanged")
//            break
        case .reconnectSuggested(_):
            DDLogDebug("websocket reconnectSuggested")
            break
        case .cancelled:
            DDLogDebug("websocket is canceled")
            isWsOpen = false
            break
        case .error(let error):
            DDLogError("websocket is error, \(String(describing: error?.localizedDescription))")
            isWsOpen = false
            break
        case .viabilityChanged(_):
            DDLogDebug("websocket viablityChanged")
            break
         
        }
    }


}

